package com.ikkong.school.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.school.model.Suppliers;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.school.model.Buynum;
import com.ikkong.school.service.BuynumService;
import com.ikkong.school.service.impl.BuynumServiceImpl;
import com.ikkong.system.util.CommonUtil;

/**
 * Generated by Blade.
 * 2018-07-22 10:50:58
 */
public class BuynumController extends UrlPermissController {
	private static String CODE = "buynum";
	private static String PERFIX = "buy_num";
	private static String LIST_SOURCE = "Buynum.list";
	private static String BASE_PATH = "/platform/buynum/";
	
	BuynumService service = new BuynumServiceImpl();
	
	public void index() {
        setAttr("uid",ShiroKit.getUser().getId().toString());
		setAttr("code", CODE);
		render(BASE_PATH + "buynum.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "buynum_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Buynum buynum = service.findById(id);
		setAttr("model", JsonKit.toJson(buynum));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "buynum_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Buynum buynum = service.findById(id);
		setAttr("model", JsonKit.toJson(buynum));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "buynum_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}
	public void tjsh() {
        String pic = getPara("pic");
        int id = getParaToInt("id",0);
        Buynum b = new Buynum();
        b.setId(id);
        b.setPic(pic);
        b.setDate(new Date());
        b.setUid((int)ShiroKit.getUser().getId());
        b.setStatus(1);
        boolean update = service.update(b);
        if (update) {
            renderJson(success("提交成功"));
        } else {
            renderJson(error("提交失败"));
        }
    }


	public void shouhuo() {
		setAttr("code",CODE);
		render(BASE_PATH + "shouhuo.html");
	}

	public void shouhuodetail() {
		setAttr("id",getPara("id", "0"));
		setAttr("did",getPara("did", "0"));
		setAttr("code",CODE);
		render(BASE_PATH + "shouhuodetail.html");
	}

	public void shouhuodetaillist() {
		String id = getPara("id", "0");
		String did = getPara("did", "0");
		String status = getPara("status", "3");
		List<Map> list = Blade.dao().select("Buynum.shouhuodetaillist", Map.class, Record.create().set("buynum", id).set("school",did).set("status",status));
		renderJson(CommonUtil.fenye(list,getParaToInt("page",1),getParaToInt("row",20)));
	}


	public void shouhuolist() {
		String deptid = ShiroKit.getUser().getDeptId().toString();
		List<Map> list = Blade.dao().select("Buynum.shouhuoorder", Map.class, Record.create().set("school", deptid));
		renderJson(CommonUtil.fenye(list,getParaToInt("page",1),getParaToInt("row",20)));
	}


	public void beihuo() {
		setAttr("code",CODE);
		render(BASE_PATH + "beihuo.html");
	}

	public void beihuodetail() {
		setAttr("id",getPara("id", "0"));
		setAttr("did",getPara("did", "0"));
		setAttr("code",CODE);
		render(BASE_PATH + "beihuodetail.html");
	}

	public void beihuolist() {

		String deptid = ShiroKit.getUser().getDeptId().toString();
		Suppliers suppliers = Blade.create(Suppliers.class).findFirstBy("d_id = " + deptid, Suppliers.class);
		int suppid = suppliers!=null?suppliers.getId():0;
		List<Map> list = Blade.dao().select("Buynum.buylist", Map.class, Record.create().set("suppid", suppid));
		renderJson(CommonUtil.fenye(list,getParaToInt("page",1),getParaToInt("row",20)));
	}

	public void beihuodetaillist() {
		String id = getPara("id", "0");
		String did = getPara("did", "0");
        String string = ShiroKit.getUser().getDeptId().toString();
        Suppliers supp = Blade.create(Suppliers.class).findFirstBy("d_id = " + string, Suppliers.class);
        int suppid = 0;
        if(supp != null){
            suppid = supp.getId();
        }
        String status = getPara("status", "0");
        List<Map> list = Blade.dao().select("Buynum.buydetaillist", Map.class, Record.create().set("buynum", id).set("school",did).set("status",status).set("suppid",suppid));
		renderJson(CommonUtil.fenye(list,getParaToInt("page",1),getParaToInt("row",20)));
	}

	public void save() {
		Buynum buynum = mapping(PERFIX, Buynum.class);
		boolean temp = service.save(buynum);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Buynum buynum = mapping(PERFIX, Buynum.class);
		boolean temp = service.update(buynum);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
